package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.OrderBO;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.OrderDao;
import com.jackiechancinema.ssm.dao.OrderDateDao;
import com.jackiechancinema.ssm.dto.OrderExcelDto;
import com.jackiechancinema.ssm.entity.Admin;
import com.jackiechancinema.ssm.entity.Order;
import com.jackiechancinema.ssm.entity.OrderDate;
import com.jackiechancinema.ssm.listener.OrderReadListener;
import com.jackiechancinema.ssm.service.Inte.OrderService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderReadListener listener;
    @Autowired
    private OrderDateDao orderDateDao;

    @Override
    public PageVo<Order> page(int page, int limit) {
        PageHelper.startPage(page, limit);
        //2.获取分页的查询结果
        PageInfo<Order> pageInfo=new PageInfo<>(orderDao.queryAll());
        System.out.println(pageInfo.getList());
        return new PageVo<Order>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R delete(int id, HttpSession session) {
        int num = orderDao.delete(id);
        if(num>0){
            Admin admin = (Admin)session.getAttribute("curruser");
            OrderDate orderDate = new OrderDate(admin.getAdmin_name(), SystemContant.ORDER_DATAE_DELETE, new Date());
            orderDateDao.insert(orderDate);
            return R.ok("删除成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public R add(Order order,HttpSession session) {
        System.out.println(order);
        order.setFilmDate(new Date());

        int num = orderDao.insert(order);
        if(num>0){
            Admin admin = (Admin)session.getAttribute("curruser");
            OrderDate orderDate = new OrderDate(admin.getAdmin_name(), SystemContant.ORDER_DATAE_INSERT, new Date());
            orderDateDao.insert(orderDate);
            return R.ok("添加成功");
        }else{
            return R.fail();
        }
    }

    @Override
    public PageVo<Order> page(OrderBO bo) {
        if(bo!=null){
            //1.组装查询条件
            Order sc=new Order();
            if(StringUtils.hasLength(bo.getMoviceName())){
                sc.setMovice(bo.getMoviceName());
            }
            if(StringUtils.hasLength(bo.getHallName())){
                sc.setHall(bo.getHallName());
            }
            if(StringUtils.hasLength(bo.getClient())){
                sc.setClient(bo.getClient());
            }
            //2.开启分页插件
            PageHelper.startPage(bo.getPage(), bo.getLimit());
            //3.获取分页的查询结果
            PageInfo<Order> pageInfo=new PageInfo<>(orderDao.queryAllPlus(sc));
            //4.返回结果
            return new PageVo<Order>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Order>(SystemContant.PAGE_R,"OK",0,null);
    }

    @Override
    public R batchadd(MultipartFile file,HttpSession session) {
        if(!file.isEmpty()){
            //excel解析
            try {
                EasyExcel.read(file.getInputStream(), OrderExcelDto.class,listener).sheet().doRead();

                Admin admin = (Admin)session.getAttribute("curruser");
                OrderDate orderDate = new OrderDate(admin.getAdmin_name(), SystemContant.ORDER_DATAE_ADDBATCH, new Date());
                orderDateDao.insert(orderDate);

                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(OrderBO bo, HttpServletResponse response) {
        String fn = System.currentTimeMillis()+".xlsx";


        response.setHeader("content-disposition","attachment;filename="+fn);

        //2.组装查询条件
        Order sc=new Order();
        if(StringUtils.hasLength(bo.getMoviceName())){
            sc.setMovice(bo.getMoviceName());
        }
        if(StringUtils.hasLength(bo.getHallName())){
            sc.setHall(bo.getHallName());
        }
        if(StringUtils.hasLength(bo.getClient())){
            sc.setClient(bo.getClient());
        }
        //3.获取要下载的数据
        List<Order> list=orderDao.queryAllPlus(sc);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Order.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public R update(Order order,HttpSession session) {
        int num = orderDao.update(order);
        if(num>0){
            Admin admin = (Admin)session.getAttribute("curruser");
            OrderDate orderDate = new OrderDate(admin.getAdmin_name(), SystemContant.ORDER_DATAE_UPDATE, new Date());
            orderDateDao.insert(orderDate);
            return R.ok("修改成功");

        }else{
            return R.fail();
        }

    }
}
